<template>
  <div class="myContainer">        
     <el-row class="myTitleBorderBottom">
      <el-col :span="20" class="myLineHeight_40px">
        基本信息
      </el-col>
    </el-row>
    <el-form :model="detail" :rules="rules" ref="ruleForm">   	
       <el-row class="myMarginTop_10px">
     <el-col :span="3" class="myLeftMsg">    	
       所属小区：
      </el-col>
      <el-col :span="7">
      	
        <template>
          <el-select 
            v-model="area_id"
             :disabled="is_modifly"
            clearable
            placeholder="所属小区请选择">
            <el-option
              v-for="item in areaIdList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>        
      </el-col>
    </el-row> 
    
    <el-row class="myMarginTop_10px">
     <el-col :span="3" class="myLeftMsg">    	
       绑定状态：
      </el-col>
      <el-col :span="7">      	
        <template>
          <el-select 
            v-model="bind_status"
             :disabled="is_modifly"
            clearable
            placeholder="">
            <el-option
              v-for="item in bindStatusList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>        
      </el-col>
    </el-row> 	
    
    
   <el-row class="myMarginTop_10px">
      <el-col :span="3" class="myLeftMsg">       
         绑定用户手机号:
      </el-col>
      <el-col :span="7">     	
           <el-input placeholder="请输入绑定用户手机号" v-model="bind_user_phone" :readonly="is_modifly"></el-input>        
      </el-col>
    </el-row>
    
    <el-row class="myMarginTop_10px">
     <el-col :span="3" class="myLeftMsg">    	
       卡类型：
      </el-col>
      <el-col :span="7">      	
        <template>
          <el-select 
            v-model="card_type"
             :disabled="is_modifly"
            clearable
            placeholder="">
            <el-option
              v-for="item in cardTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>        
      </el-col>
    </el-row> 	
  
   <el-row class="myMarginTop_10px">
     <el-col :span="3" class="myLeftMsg">    	
       是否启用：
      </el-col>
      <el-col :span="7">      	
        <template>
          <el-select 
            v-model="data_status"
             :disabled="is_modifly"
            clearable
            placeholder="">
            <el-option
              v-for="item in dataStatusList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>        
      </el-col>
    </el-row> 
  
     <el-row class="myMarginTop_10px">
      <el-col :span="3" class="myLeftMsg">       
         物理卡号:
      </el-col>
      <el-col :span="7">     
           <el-input placeholder="请输入物理卡号" v-model="epc_Id" :readonly="is_modifly"></el-input>    
      </el-col>
    </el-row>
    
    <el-row class="myMarginTop_10px">
      <el-col :span="3" class="myLeftMsg">       
       逻辑卡号  :
      </el-col>
       
      <el-col :span="7">  
      	 
           <el-input placeholder="请输入逻辑卡号" v-model="card_no" :readonly="is_modifly"></el-input>    
        
      </el-col>
    </el-row> 
    
     <el-row class="myMarginTop_10px">
      <el-col :span="3" class="myLeftMsg">       
       备注  :
      </el-col>
      <el-col :span="7">      	
           <el-input placeholder="请输入备注" v-model="extra_desc" type="textarea" :readonly="is_modifly"></el-input>      
      </el-col>
    </el-row>  
    <el-row class="myMarginTop_10px">
      <el-col :span="7" :offset="3">
        <el-button type="primary" class="myBgGreen" v-if="type=='update'" @click="modify">保 存</el-button>
        <el-button type="primary" class="myBgGreen" v-if="type=='add'" @click="onCreate">新 增</el-button>
        <el-button type="primary" class="myBgGreen" @click="onBack">返 回</el-button>
      </el-col>
    </el-row>
  </el-form>  
    
  </div>
</template>
<style scoped>
  .myContainer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }
  .myLeftMsg{
    line-height: 40px;
    text-align: right;
    padding-right: 10px;
  }
  .myMarginTop_10px{
    padding-bottom: 10px;
  }
  .myVA_M{
    vertical-align: middle;
  }
  .myMarginBottom_10px{
    margin-bottom: 20px;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border:1px solid #d8dce5;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<script>
  import { getCardDetail , editCard , addCard } from '@/api/entrance_guard';
  import { r_email , r_phone } from "@/api/regular";//正则
  export default {
    data(){
      return{
        title: "门禁卡管理",
        id: "",
        is_modifly:true,
        type:"",
        ad_name:"",
        detail:{
          
        }, 

        area_id:"",
        bind_status:"",
      
        bind_user_phone:"",
        card_id:"",
        card_no:"",
        card_type:"",
        data_status:"",
        epc_Id:"",
        extra_desc:"",
        areaIdList:[{value:1,label:"鸥鹏k城"},{value:2,label:"壹号公馆"},{value:3,label:"龍腾盛世"},{value:4,label:"北城蓝湖"},{value:7,label:"盛世华城"},{value:6,label:"学府雅苑"}],
        bindStatusList:[{value:0,label:"未使用"},{value:1,label:"已使用"}],
        cardTypeList:[{value:1,label:"卡片"},{value:2,label:"身份证"},{value:3,label:"银行卡"}],
        dataStatusList:[{value:0,label:"失效"},{value:1,label:"启用"}],
        rules: {         
          epc_id:[
            { required: true, message: '请输入卡号', trigger: 'blur' },
            { min: 1, max: 8, message: '长度为8个字符', trigger: 'blur' }
          ]         
        },
      }
    },
    components: {
        
    },
    created(){
      var vm = this;
      vm.type = vm.$route.query.type;
      vm.id = vm.$route.query.id;

      if(vm.type=='update'){ //修改
        vm.is_modifly = false;
        vm.onGetAd();
      }
      if(vm.type=='info'){ //修改
        vm.onGetAd();
      }
      if(vm.type=='add'){
        vm.is_modifly = false;
      }
    },
    methods:{   
    	
     modify(){
        let vm = this;
        let id = vm.card_id;
        let params={              
          area_id:vm.area_id,
          bind_user_phone:vm.bind_user_phone,                  
          card_no:vm.card_no,
          card_type:vm.card_type,
          data_status:vm.data_status,
          epc_Id:vm.epc_Id,
          extra_desc:vm.extra_desc
        }
        editCard(vm.id, params).then(response => {
          console.log("修改成功")
          this.$message.success('执行成功！');
          this.$router.go(-1);
        }).catch(error => {
          this.$message.error('执行失败！');
          console.log("修改失败")
        })
      },
      onCreate(){//添加
        let vm=this;
        let params={        
          area_id:vm.area_id,                 
          card_no:vm.card_no,
          card_type:vm.card_type,
          data_status:vm.data_status,
          epc_Id:vm.epc_Id,
          extra_desc:vm.extra_desc
        }
        addCard(params).then(response => {
          let res=response.data;
          vm.$message.success("添加数据成功！");
          this.$router.go(-1);
          console.log("添加成功",res);
        }).catch(error => {
          vm.$message.error("添加数据失败！");
          console.log("添加失败",error);
        })
        console.log(params)
      },
      onBack(){//返回
      	let vm =this;
      	window.localStorage.setItem('pageNum',vm.$route.query.pageNum||1);
        this.$router.go(-1);
      },
      
      onGetAd(){//拉取
          let vm=this;      
          getCardDetail(vm.id).then(response => {
          let res=response.data;    
          vm.bind_status=res.bind_status;
          vm.bind_user_phone=res.bind_user_phone;
          vm.area_id=Number(res.area_id);
          vm.card_no=res.card_no;
          vm.card_type=Number(res.card_type);
          vm.data_status=Number(res.data_status);
          vm.epc_Id=res.epc_Id;
          vm.extra_desc=res.extra_desc;         
          vm.card_id=res.card_id;
        }).catch(error => {
          console.log("拉取数据失败",error);
        })
      } 
      

    }
  
  }


</script>



